import React from "react";
import { useNavigate } from "react-router-dom";
import { Button, Input, Form, Toast } from "react-vant";
import axios from "../utils/request";
import LoginThree from "../components/LoginThree";
type Props = {};

const Login = (props: Props) => {
  const navigate = useNavigate();
  const [form] = Form.useForm();

  const onFinish = (values: any) => {
    (async () => {
      let res = await axios.post("/login", values);
      if (res.data.code === 200) {
        Toast.success("登录成功");
        localStorage.setItem("token", res.data.token);
        localStorage.setItem("username", values.username);
        navigate("/index/my");
      } else {
        Toast.fail(res.data.msg);
      }
    })();
  };
  //跳转注册页面
  const goRegister = () => {
    navigate("/register");
  };
  return (
    <div className="login">
      <header>登录页</header>
      <Form
        form={form}
        onFinish={onFinish}
        footer={
          <div style={{ margin: "16px 16px 0" }}>
            <Button round nativeType="submit" className="tm" block>
              登录
            </Button>
            <div style={{ width: "100%", height: "30px" }}></div>
            <Button onClick={goRegister} round className="tm" block>
              去注册
            </Button>
          </div>
        }
      >
        <Form.Item
          rules={[{ required: true, message: "请填写用户名" }]}
          name="username"
          label="用户名"
        >
          <Input placeholder="请输入用户名" />
        </Form.Item>
        <Form.Item
          rules={[{ required: true, message: "请填写密码" }]}
          name="password"
          label="密码"
        >
          <Input placeholder="请输入密码" />
        </Form.Item>
      </Form>
      <LoginThree />
    </div>
  );
};

export default Login;
